package com.bootdo.put.service.impl;

import com.bootdo.goods.dao.TGoodsAllocationAloneDao;
import com.bootdo.goods.dao.TGoodsAllocationDao;
import com.bootdo.goods.domain.TGoodsAllocationAloneDO;
import com.bootdo.goods.service.TGoodsAllocationService;
import com.bootdo.put.dao.PutStorageDetailDao;
import com.bootdo.put.domain.PutStorageDetailDO;
import com.bootdo.put.service.PutStorageDetailService;
import com.bootdo.stock.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.*;

import com.bootdo.goods.domain.TGoodsAllocationDO;
import com.bootdo.put.dao.PutStorageDao;
import com.bootdo.put.domain.PutStorageDO;
import com.bootdo.put.service.PutStorageService;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class PutStorageServiceImpl implements PutStorageService {
	@Autowired
	private PutStorageDao putStorageDao;
	@Autowired
	private TGoodsAllocationDao tGoodsAllocationDao;
	@Autowired
	private PutStorageDetailDao putStorageDetailDao;
	@Autowired
	private TGoodsAllocationAloneDao tGoodsAllocationAloneDao;
	@Autowired
	private StockService stockService;
	
	@Override
	public PutStorageDO get(Long putId){
		return putStorageDao.get(putId);
	}
	
	@Override
	public List<PutStorageDO> list(Map<String, Object> map){
		return putStorageDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return putStorageDao.count(map);
	}
	
	@Override
	public int save(PutStorageDO putStorage){
		return putStorageDao.save(putStorage);
	}
	
	@Override
	public int update(PutStorageDO putStorage){
		return putStorageDao.update(putStorage);
	}

	@Override
	public List<PutStorageDO> listStatus() {
		
		return putStorageDao.listStatus();
	}

	@Override
	public List<PutStorageDO> listByStatus(String status) {
		
		 Map<String, Object> param = new HashMap<>();
	        param.put("status", status);
	        return putStorageDao.list(param);

	}

	@Override
	public int updateSpecial(PutStorageDO putStorageDO,String allocationNumber) {
        Map<String,Object> map = new HashMap<>();
        map.put("providerId",putStorageDO.getProviderId());

        if(putStorageDO.getPutType().equals("142003")){
         map.put("allocationType","122005");
		}else if(putStorageDO.getPutType().equals("142002")){
			map.put("allocationType","122003");
		}else {
            return -2;
        }

        List<TGoodsAllocationDO> tGoodsAllocationDOList= tGoodsAllocationDao.list(map);
		System.out.println(tGoodsAllocationDOList.get(0).getAllocationNumber().equals(allocationNumber));
        if(tGoodsAllocationDOList.size()==0){
        	return -1;
		}if(!tGoodsAllocationDOList.get(0).getAllocationNumber().equals(allocationNumber)){
			return -1;
		}
		tGoodsAllocationDOList.get(0).getAllocationNumber();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("putId",putStorageDO.getPutId());
        List<PutStorageDetailDO> list = putStorageDetailDao.list(map1);

		TGoodsAllocationAloneDO tGoodsAllocationAloneDO = null;
		Map<String,Object> map2 = new HashMap<>();


		for (PutStorageDetailDO putStorageDetailDO: list) {
			map2.put("allocationNumber",allocationNumber);
			map2.put("goodsId",putStorageDetailDO.getGoodsId());
			tGoodsAllocationAloneDO=tGoodsAllocationAloneDao.allocationGoods(map2);
			stockService.update(putStorageDetailDO.getGoodsId(),putStorageDetailDO.getGoodsNumber(),0,0,0);


			if(tGoodsAllocationAloneDO!=null){

				tGoodsAllocationAloneDO.setGoodsNumber(tGoodsAllocationAloneDO.getGoodsNumber()+putStorageDetailDO.getGoodsNumber());
                tGoodsAllocationAloneDao.update(tGoodsAllocationAloneDO);
			}else {
				tGoodsAllocationAloneDO=new TGoodsAllocationAloneDO();
				tGoodsAllocationAloneDO.setCreateDate(new Date());
				tGoodsAllocationAloneDO.setId(UUID.randomUUID().toString());
				tGoodsAllocationAloneDO.setAllocationNumber(allocationNumber);
                tGoodsAllocationAloneDO.setGoodsNumber(putStorageDetailDO.getGoodsNumber());
				tGoodsAllocationAloneDO.setGoodsId(putStorageDetailDO.getGoodsId());

				tGoodsAllocationAloneDao.save(tGoodsAllocationAloneDO);
			}



           putStorageDetailDO.setPutDetailStatus("121003");
           putStorageDetailDao.update(putStorageDetailDO);
		}
		putStorageDO.setStatus("140002");
		return putStorageDao.update(putStorageDO);
	}

//	@Override
//	public int remove(Long putId){
//		return putStorageDao.remove(putId);
//	}
//	
//	@Override
//	public int batchRemove(Long[] putIds){
//		return putStorageDao.batchRemove(putIds);
//	}

}
